package com.mvp.shq.washer.ui.main;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.mvp.shq.washer.APP;
import com.mvp.shq.washer.entity.Banner;
import com.mvp.shq.washer.entity.result.BannerResult;
import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.entity.VersionBean;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.jpush.TagAliasOperatorHelper;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * Created by Administrator on 2017/8/19.
 */

public class MainPresenter extends MainConstract.Presenter {
    @Override
    public void onStart() {
        getVersion();
        if (SpUtil.getUser() != null){
            setAlias();
        }
    }

    @Override
    void setAlias() {
        int sequence = 0;
        String alias = SpUtil.getUser().getId();
        TagAliasOperatorHelper.TagAliasBean bean = new TagAliasOperatorHelper.TagAliasBean();
        bean.setAliasAction(true);
        bean.setAlias(alias);
        bean.setAction(TagAliasOperatorHelper.ACTION_SET);
        TagAliasOperatorHelper.getInstance().handleAction(APP.getAppContext(),sequence,bean);
    }

    @Override
    void getUserInfo() {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        Logger.d("token："+token);
        Logger.d("userId:"+userId);
        model.getUserInfo(token,userId)
                .subscribe(new BaseObserver<User>(view) {
                    @Override
                    protected void onSuccees(BaseResult<User> t) throws Exception {
                        if (!t.isError()){
                            SpUtil.setUser(t.getData());
                            RxBus.get().send(EventCode.UPDATE_USER);
                        }else {
                            T.show(t.getMsg());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show("获取用户信息失败");
                        e.printStackTrace();
                        Logger.e("获取用户信息失败:");
                    }
                });
    }

    @Override
    void getVersion() {
        String platform = "0";
        model.version(platform).subscribe(new BaseObserver<VersionBean>(view) {
            @Override
            protected void onSuccees(BaseResult<VersionBean> t) throws Exception {
                if (!t.isError()){
                    PackageManager manager = APP.getApp().getPackageManager();
                    PackageInfo info = null;
                    try {
                        info = manager.getPackageInfo(APP.getApp().getPackageName(), 0);
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                    }
                    String version = info.versionName;
                    if (t.getData() == null){
                        return;
                    }
                    if (!version.equals(t.getData().getVersionName())){
                        view.updateAPK(t.getData());
                    }
                }
            }

            @Override
            protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

            }
        });
    }

    @Override
    void banner() {
        model.banner()
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<List<Banner>>(view) {
                    @Override
                    protected void onSuccees(BaseResult<List<Banner>> t) throws Exception {
                        if (!t.isError()){
                            SpUtil.setBanner(t.getData());
                            RxBus.get().send(EventCode.UPDATE_BANNER);
                        }else {

                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }
                });
    }

}
